package com.yjs.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yjs.bean.Goods;
import com.yjs.bean.PageBean;
import com.yjs.bean.Store;
import com.yjs.mapper.StoreMapper;
import com.yjs.service.StoreService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yjs
 * @create 2020-09-17-12:06
 * 仓储实现层
 */
@Service
public class StoreServiceImpl implements StoreService {
    //注入仓储mapper
    @Autowired
    StoreMapper storeMapper;

    @Override
    public List<Store> getAllStore() {
        List<Store> allStore = storeMapper.getAllStore();
        if (allStore.size()>0 && allStore!=null){
            return allStore;
        }
        return null;
    }

    //查询所有仓储并分页
    @Override
    public PageBean<Store> findAllByPages(String _currentPage,String _rows) {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);
        //创建一个空的pageBean
        PageBean<Store> pbStore = new PageBean<>();
        //2,设置参数
        pbStore.setCurrentPage(currentPage);
        pbStore.setRows(rows);
        //3,根据mapper查询总记录数
        int totalCount=storeMapper.findTotalCount();
        pbStore.setTotalCount(totalCount);
        //4,调用mapper查询list集合
        //计算开始的记录索引
        int start =(currentPage - 1) * rows;
        Map<String,Integer> map = new HashMap<>();
        map.put("start",start);
        map.put("rows",rows);
        System.out.println(map);
        List<Store> list= storeMapper.findByPage(map);
        pbStore.setList(list);
        //5计算总页码
        int totalPage = (totalCount % rows) == 0 ? (totalCount/rows) : (totalCount/rows) + 1;
        pbStore.setTotalPage(totalPage);
        return  pbStore;
    }
    //根据id查询仓储
    @Override
    public Store getOneStore(Integer sid) {
        Store oneStore = storeMapper.getOneStore(sid);
        return oneStore==null?null:oneStore;
    }
    //添加仓储
    @Override
    public void addStore(Store store) {
        storeMapper.addStore(store);
    }
    //删除仓储
    @Override
    public void delStore(Integer sid) {
        storeMapper.delStore(sid);
    }
    //修改仓储
    @Override
    public void updStore(Store store) {
        storeMapper.updStore(store);
    }
}
